var app = getApp();
var ux = require('../../../lib/ux.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchant:{},
    sumCount:0,
    next: true,
    merchantName:'',
    list: [],
    page: 0,
    size: 10,
    isShow:true,
    submitUrl:"/api/balance/carConfirmOrder.do",
    action: "/api/memberShop/carList.do",
    submit: "{{submit}}"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      merchant: wx.getStorageSync("banrdId"),
      merchantName: wx.getStorageSync("banrd").brandName
    })
    var self = this;
   
    ux.login(function (protocol) {
      if (protocol.success) {
        wx.setStorageSync("userInfo", protocol.data.userInfo);
        wx.setStorageSync("token", protocol.data.token);
        self.setData({
          userInfo: protocol.data.userInfo
        })
      }
    })
  },

  onForm: function (event) {
    if (this.data.submit) {
      var $this = this;
      var dataset = event.currentTarget.dataset;
      ux.request(this.data.submit, detail.value, function (protocol) {
        if (protocol.success) {
          if (dataset.url) {
            wx.redirectTo(dataset.url);
          }
        }
      }, true);
    }
  },

  onTap: function (event) {
    var dataset = event.currentTarget.dataset;
    var data = this.data;
    if (dataset.key && dataset.val) {
      data[dataset.key] = dataset.val;
    }
    this.setData(data);
  },
  shanchu: function (event){
    var self=this;
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    var lists = this.data.list;
    var pamars={id:lists[index].id}
    wx.showLoading({
      title: '删除中...',
    })
    ux.request("/api/memberShop/deletCar.do", pamars, function (protocol) {
      wx.hideLoading();
      if(protocol.data.type){
        self.toast("删除成功");
        self.setData({
          isShow:true
        })
        self.onShow();
      }else{
        self.toast("删除失败");
      }
    }, true)
  },
  jianCount: function (event){
    var dataset = event.currentTarget.dataset;
       var index=dataset.index;
       var lists=this.data.list;
    if (lists[index].count==1){
      return false;
    }else{
      lists[index].count = lists[index].count-1;
      if (lists[index].select==1){
        var num = 0;
        for (var i = 0; i < lists.length; i++) {
          if (lists[i].select == 1) {
            num = ux.acAdd(num, ux.acMul(lists[i].price, lists[i].count))
          }
        }
        this.setData({
          sumCount: num,
        })
      }
      this.setData({
        list:lists
      })
    }
  },
  jiaCount: function (event) {
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    var lists = this.data.list;
      lists[index].count = lists[index].count + 1;
    if (lists[index].select == 1) {
      var num = 0;
      for (var i = 0; i < lists.length; i++) {
        if (lists[i].select == 1) {
          num = ux.acAdd(num, ux.acMul(lists[i].price, lists[i].count))
        }
      }
      this.setData({
        sumCount: num,
      })
    }
      this.setData({
        list: lists
      })
  },
  selects: function (event) {
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    var lists = this.data.list;
    if (lists[index].select==1){
      lists[index].select=0
       var num=0;
      for (var i = 0; i < lists.length; i++) {
        if (lists[i].select == 1) {
          num = ux.acAdd(num, ux.acMul(lists[i].price, lists[i].count))
        }
      }

      this.setData({
        sumCount: num,
        list:lists
      })
     
    }else{
      lists[index].select = 1
      var num = 0;
      for (var i = 0; i < lists.length; i++) {
        if (lists[i].select == 1) {
          num = ux.acAdd(num, ux.acMul(lists[i].price, lists[i].count))
        }
      }
      this.setData({
        sumCount: num,
        list: lists
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   if(this.data.isShow){
     this.setData({
       sumCount: 0,
       next: true,
       list: [],
       page: 0,
       size: 10,
       isShow:false
     })
     this.getList();
   }
  },
  getList: function () {
    var self = this;
    if (self.data.next == false) {
      wx.showToast({
        title: '没有更多了',
        icon: 'loading',
        duration: 1500
      });
      return;
    }
    wx.showLoading({
      mask: true,
      title: '加载中....'
    })
    if (!self.data.merchant){
      wx.reLaunch({
        url: '/pages/brandSelect',
      })
    }
    var params = { banrdId: self.data.merchant, pageNo: parseInt(self.data.page), pageSize: self.data.size };
    ux.request(self.data.action, params, function (data) {
      var datas = data.data.list;
      if (data.data.pageInfo.next == true) {
        self.setData({
          next: true
        });
      } else {
        self.setData({
          next: false
        });
      }
      if (self.data.page == 0) {
        for (var i = 0; i < datas.length; i++) {
          datas[i].select = 1
        }
        self.setData({
          list: datas
        });
        var lists = self.data.list;
        var num = 0;
        for (var i = 0; i < lists.length; i++) {
          if (lists[i].select == 1) {
            num = ux.acAdd(num, ux.acMul(lists[i].price, lists[i].count))
          }
        }
        self.setData({
          sumCount: num
        });
      } else {
        for (var i = 0; i < datas.length; i++) {
          datas[i].select = 1
        }
        self.setData({
          list: self.data.list.concat(datas)
        });
        var lists=self.data.list;
        var num = 0;
        for (var i = 0; i < lists.length; i++) {
          if (lists[i].select == 1) {
            num = num + lists[i].price * lists[i].count
          }
        }
        self.setData({
          sumCount: num
        });
      }
      var pages = self.data.page + 1;
      self.setData({
        page: pages
      });
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      isShow: true
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList();
  },

  toGoconfirm:function(){
   var self=this;
   var lists=this.data.list;
   var isFlag=false;
    for (var i = 0; i < lists.length; i++) {
      if (lists[i].select == 1) {
        isFlag=true;
        break;
      }
    }
    if(!isFlag){
     self.toast("请选择商品")
      return false;
    }
    wx.showLoading({
      title: '处理中....',
    })
    var ids = "";
    var counts = "";
    var pas=[];
    for (var i = 0; i < lists.length; i++) {
      if (lists[i].select == 1) {
        var item={};
        item.id = lists[i].id;
        item.count = lists[i].count;
        pas.push(item);
      }
    }
    var params = { banrdId: parseInt(self.data.merchant), jsons: pas};
    ux.request(self.data.submitUrl, params, function (data) {
      wx.hideLoading();
      if (data.success) {
        wx.navigateTo({
          url: '/pages/member/tab2/confirmZhiOrder?orderId=' + data.data.orderId,
        })
      } else {
        self.toast("提交失败");
      }
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    });
   

  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  }
})
